package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.entity.vo.SearchObjVo;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.atguigu.staservice.utils.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.crypto.DSA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Permission;
import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author AIR
 * @since 2022-10-14
 */
@Service
@Transactional
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {

        //添加日期之前删除表相同日期的数据
        LambdaQueryWrapper<StatisticsDaily> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StatisticsDaily::getDateCalculated, day);
        baseMapper.delete(wrapper);

        //远程调用得到某一天的注册认数
        R registerR = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) registerR.getData().get("countRegister");

        //第一种方法
/*        LambdaQueryWrapper<StatisticsDaily> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StatisticsDaily::getDateCalculated,day);
        StatisticsDaily selectOne = baseMapper.selectOne(wrapper);
        if (day.equals(selectOne.getDateCalculated())){
            selectOne.setRegisterNum(countRegister);
            baseMapper.updateById(selectOne);
            return;
        }*/

        //第二种方法

        //把获取数据添加到添加数据表里，统计分析表里面
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(countRegister);//注册人数
        sta.setDateCalculated(day);//统计日期

        sta.setVideoViewNum(RandomUtils.nextInt(100, 300));
        sta.setCourseNum(RandomUtils.nextInt(100, 300));
        sta.setLoginNum(RandomUtils.nextInt(100, 300));

        baseMapper.insert(sta);
    }

    //返回最近七天的数据
    //图标显示，返回两部分数据，日期json数组，数量json数组
    @Override
    public Map<String, Object> getShowData(SearchObjVo searchObjVo) {

        //根据条件查询对应的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(searchObjVo.getBegin())) {
            wrapper.ge("date_calculated", searchObjVo.getBegin());
        }
        if (!StringUtils.isBlank(searchObjVo.getEnd())) {
            wrapper.le("date_calculated", searchObjVo.getEnd());
        }
        wrapper.orderByAsc("date_calculated");
        wrapper.select("date_calculated", searchObjVo.getType());

        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        if (Objects.isNull(staList)){
            throw new GuliException(20001, "数据为空");
        }

        //因为返回有两部分数据：日期 和 日期对应数量
        //前端要求数组json结构，对应后端java代码是list集合
        //创建两个list集合，一个日期list，一个数量list
        List<String> dateList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        //遍历查询所有数据list集合，进行封装
        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily daily = staList.get(i);
            System.out.println(i);
            //封装日期list集合
            dateList.add(daily.getDateCalculated());
            //封装对应数量
            switch (searchObjVo.getType()) {
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        //把封装之后两个list集合放到map集合，进行返回
        Map<String, Object> map = new HashMap<>();
        map.put("dateList", dateList);
        map.put("numDataList", numDataList);

        return map;
    }

    //返回最近七天的数据
    @Override
    public Map<String, Object> getNearlySevenDays() {

        //对七天的数据进行一个升序
        LambdaQueryWrapper<StatisticsDaily> wrapper = new LambdaQueryWrapper<>();
        String orderDate = DateUtil.formatDate(DateUtil.addDays(new Date(), -8));
        String currentDate = DateUtil.formatDate(DateUtil.addDays(new Date(), -1));
        wrapper.between(StatisticsDaily::getDateCalculated,orderDate,currentDate);
        wrapper.orderByAsc(StatisticsDaily::getDateCalculated);

        List<StatisticsDaily> sevenList = baseMapper.selectList(wrapper);

        if (Objects.isNull(sevenList)){
            throw new GuliException(20001, "近七天数据为空");
        }
        List<String> dateList = new ArrayList<>();
        List<Integer> numLoginList = new ArrayList<>();
        List<Integer> numCourseList = new ArrayList<>();
        List<Integer> numRegisterList = new ArrayList<>();
        List<Integer> numVideoViewList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();

        for (int i = 0; i < sevenList.size(); i++) {
            StatisticsDaily daily = sevenList.get(i);
            dateList.add(daily.getDateCalculated());
            numLoginList.add(daily.getLoginNum());
            numCourseList.add(daily.getCourseNum());
            numRegisterList.add(daily.getRegisterNum());
            numVideoViewList.add(daily.getVideoViewNum());

        }
        map.put("dateList",dateList);
        map.put("numLoginList", numLoginList);
        map.put("numCourseList", numCourseList);
        map.put("numRegisterList", numRegisterList);
        map.put("numVideoViewList", numVideoViewList);
        return map;
    }

    //返回前一天数据
    @Override
    public Map<String, Object> getPreviousDayData() {
        String previousDate = DateUtil.formatDate(DateUtil.addDays(new Date(), -1));
        LambdaQueryWrapper<StatisticsDaily> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StatisticsDaily::getDateCalculated,previousDate);
        StatisticsDaily statisticsDaily = baseMapper.selectOne(wrapper);

        if (Objects.isNull(statisticsDaily)){
            throw new GuliException(20001, "数据为空");
        }

        List<String> dateList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        dateList.add(statisticsDaily.getDateCalculated());
        numDataList.add(statisticsDaily.getRegisterNum());
        numDataList.add(statisticsDaily.getLoginNum());
        numDataList.add(statisticsDaily.getVideoViewNum());
        numDataList.add(statisticsDaily.getCourseNum());

        Map<String,Object> map = new HashMap<>();
        map.put("dateList",dateList);
        map.put("numDataList",numDataList);

        return map;
    }


}
